﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace ALE_Server
{
    static class XmlSerializeHelper
    {
        public static string Serialize(object obj)
        {
            MemoryStream Stream = new MemoryStream();

            XmlSerializer Serializer = new XmlSerializer(obj.GetType());
            Serializer.Serialize(Stream, obj);

            Stream.Seek(0, SeekOrigin.Begin);
            string Result =  System.Text.Encoding.Default.GetString(Stream.GetBuffer());

            Stream.Close();

            return Result;
        }

        public static object Deserialize(string xmlStr, Type type)
        {
            MemoryStream Stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(xmlStr));
            Stream.Seek(0, SeekOrigin.Begin);

            XmlSerializer Serializer = new XmlSerializer(type);

            object Result = Serializer.Deserialize(Stream);

            Stream.Close();

            return Result;
        }            
    }
}
